package handlers;


import java.net.Socket;

import main_classes.Server;

import common.classes.HeaderWorker;

public class ServerHandler extends Handler {

	public ServerHandler() {
		super();
	}

	public ServerHandler(Handler next) {
		super(next);
	}

	@Override
	public void parseRequest(Socket socket, HeaderWorker header, String error) {
		if (error != null) {
			super.parseRequest(socket, header, error);
			return;
		}
		
		if (!header.getMethod().equals("SERVER")) {
			if (Server.isWorking()) 
				super.parseRequest(socket, header, error);
			else 
				this.closeSocket(socket);
			return;
		}

		if (!(header.getAdminName().equals(Server.getAdminName())
				&& header.getAdminPassword().equals(Server.getAdminPassword()))){
			super.parseRequest(socket, header,"401 Unauthorized");
			return;
		}

		if (header.getAdminComand().equals("START") && (!Server.isWorking()))
			Server.start();
		if (header.getAdminComand().equals("RESTART") && (Server.isWorking()))
			Server.restart();
		if (header.getAdminComand().equals("STOP"))
			Server.stop();
		
		this.closeSocket(socket);
	}
}
